package com.sky.controller.user;

import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.pojo.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.user.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 购物车控制器层
 * @author zgp
 * @version V1.0
 * @date 2025-01-27 14:58
 */
@Slf4j
@Api(tags = "购物车相关接口")
@RestController
@RequestMapping("/user/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    @ApiOperation("查询当前用户购物车信息")
    @GetMapping("/list")
    public Result<List<ShoppingCart>> getShoppingCartList(){
        log.info("查询当前用户购物车信息");
        return Result.success(shoppingCartService.getShoppingCartList());
    }

    @ApiOperation("添加购物车")
    @PostMapping("/add")
    public Result<String> addShoppingCartItem(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("添加购物车：{}", shoppingCartDTO);
        shoppingCartService.addItem(shoppingCartDTO);
        return Result.success(MessageConstant.ADD_SUCCESS);
    }

    @ApiOperation("清空购物车")
    @DeleteMapping("/clean")
    public Result<String> cleanShoppingCart(){
        log.info("清空购物车，userId = {}", BaseContext.getCurrentId());
        shoppingCartService.cleanShoppingCartByUserId();
        return Result.success(MessageConstant.DELETE_SUCCESS);
    }

    @ApiOperation("删除购物车单个商品")
    @PostMapping("/sub")
    public Result<String> subShoppingCartItem(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("删除购物车单个商品，shoppingCartDTO：{}", shoppingCartDTO);
        shoppingCartService.deleteShoppingCartItem(shoppingCartDTO);
        return Result.success(MessageConstant.DELETE_SUCCESS);
    }
}
